﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using MovieRental.EnhancedEntities;
using MovieRental.Entities;
using MovieRental.BuisnessComponents;
using MovieRental_Presentation.Helpers;
using MovieRental_Presentation.DataViews;

namespace MovieRental_Presentation
{
    /// <summary>
    /// Interaction logic for Customers.xaml
    /// </summary>
    public partial class Customers : Page
    {
        CustomersDataView _dataView = new CustomersDataView();

        public Customers()
        {
            InitializeComponent();
            DataContext = _dataView;
        }

        private void addButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
			this.NavigationService.Navigate(new NewCustomerDialog());
        }

        private void editButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Guid customerId = (customersDataGrid.SelectedItem as Customer).Entity.Id;
			this.NavigationService.Navigate(new CustomerDetails(customerId, false));
        }

        private void customersDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Guid customerId = (customersDataGrid.SelectedItem as Customer).Entity.Id;
            NavigationService.Navigate(new CustomerDetails(customerId, true));
        }

        private void backButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void searchBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Space)
                _dataView.SearchString = searchBox.Text;
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            Customer customer = (Customer)customersDataGrid.SelectedItem;
            if (!ComponentManager.Instance.CustomerComponent.CanDelete(customer.Entity))
            {
                string message = string.Format("{0} hasn't returned {1} editions. You can't delete him(her) at the moment.", customer.FullName, customer.NotReturnedEditions);
                MessageBox.Show(message, "Can't delete the customer.", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }    
            var result = MessageBox.Show("Do you really want do delete this customer?", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
                ComponentManager.Instance.CustomerComponent.Delete(customer.Entity);
        }

        private void assignButton_Click(object sender, RoutedEventArgs e)
        {
            var cart = (App.Current as App).CartDataView.CurrentCart.Entity;
            ComponentManager.Instance.LoanCartComponent.AssignCustomerToCart(cart, (customersDataGrid.SelectedItem as Customer).Entity);
        }

        private void dashboardButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new Dashboard());
        }

        private void moviesButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new Movies());
        }

        private void statisticsButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new Statistics());
        }      
    }
}
